package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("SupplierController")
public class SupplierController extends CmiEmaController {

	@RequestMapping("/listSuppliers")
	public ModelAndView listSuppliers(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = supplierPaginationService.updatePagination(request, pagination);
		Set<Supplier> suppliers = supplierPaginationService.getPaginationSuppliers(pagination);

		mav.addObject("suppliers", suppliers);
		mav.addObject("entityName", "supplier");
		mav.addObject("pagination", pagination);
		mav.setViewName("supplier/listSuppliers.jsp");
		return mav;
	}

	public String listSuppliers() {
		return "redirect:/listSuppliers";
	}

	@RequestMapping("/viewSupplier")
	public ModelAndView viewSupplier(@RequestParam Integer supplierId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("supplier", supplierDAO.findSupplierByPrimaryKey(supplierId));
		mav.setViewName("supplier/viewSupplier.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteSuppliers")
	public ModelAndView confirmDeleteSuppliers(
			@RequestParam(value = "deleteSuppliersIds", required = true) Set<Integer> deleteSuppliersIds) {
		ModelAndView mav = new ModelAndView();

		Set<Supplier> deleteSuppliers = supplierDAO.findSuppliersByIds(deleteSuppliersIds);
		Set<Supplier> referencedSuppliers = supplierService.loadReferenceSuppliers(deleteSuppliers);
		boolean hasReferenced;
		if (referencedSuppliers.size() == 0) {
			mav.addObject("deleteEntitys", deleteSuppliers);
			hasReferenced = false;
		} else {
			deleteSuppliers.removeAll(referencedSuppliers);
			mav.addObject("referencedEntitys", referencedSuppliers);
			mav.addObject("deleteEntitys", deleteSuppliers);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Supplier");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listSuppliers");
		return mav;
	}

	@RequestMapping("/deleteSupplier")
	public String deleteSupplier(@RequestParam Integer supplierId) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(supplierId);
		supplierService.deleteSupplier(supplier);
		return "forward:/listSuppliers";
	}

	@RequestMapping("/confirmDeleteSupplier")
	public ModelAndView confirmDeleteSupplier(@RequestParam Integer supplierId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("supplier", supplierDAO.findSupplierByPrimaryKey(supplierId));

		mav.setViewName("supplier/deleteSupplier.jsp");

		return mav;
	}

	@RequestMapping("/deleteSuppliers")
	public String deleteSuppliers(
			@RequestParam(value = "deleteSuppliersId", required = true) Set<Integer> deleteSuppliersId) {
		supplierService.deleteSuppliers(deleteSuppliersId);
		return "forward:/listSuppliers";
	}

	@RequestMapping("/editSupplier")
	public ModelAndView editSupplier(@RequestParam Integer supplierId) {

		ModelAndView mav = new ModelAndView();

		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(supplierId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("supplier", supplier);

		// 把所有的引用supplier对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(supplierService.loadSupplierObjectReferencedSet(supplier));

		// 将编辑页面supplier添加到mav中
		mav.setViewName("supplier/editSupplier.jsp");
		return mav;
	}

	@RequestMapping("/newSupplier")
	public ModelAndView newSupplier() {
		ModelAndView mav = new ModelAndView();
		Supplier supplier = new Supplier();

		mav.addObject("supplier", supplier);

		// 把所有的引用supplier对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(supplierService.loadSupplierObjectReferencedSet(supplier));

		mav.addObject("newFlag", true);
		mav.setViewName("supplier/editSupplier.jsp");

		return mav;
	}

	@RequestMapping("/saveSupplier")
	public ModelAndView saveSupplier(@ModelAttribute Supplier supplier) {

		ModelAndView mav = new ModelAndView();

		Address addressTemp = supplier.getAddress();
		Address address = addressTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(addressDAO.findAddressById(addressTemp.getId()));

		Supplier existingSupplier = CmiHibernateProxyUtil
				.deproxy(supplierDAO.findSupplierByPhysicalPrimaryKey(supplier.getName(), address.getId()));

		supplier.setAddress(address);
		// 新建记录
		if (supplier.getId() == null) {
			if (supplier.equals(existingSupplier)) {

				mav.addAllObjects(supplierService.loadSupplierClassReferencedSet(supplier));
				// mav = editContinue(mav, supplier);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("supplier/editSupplier.jsp");
			} else {
				supplierService.saveSupplier(supplier);
				mav.setViewName("forward:/listSuppliers");
			}
		}
		// 编辑记录
		else {
			Supplier originSupplier = CmiHibernateProxyUtil
					.deproxy(supplierDAO.findSupplierByPrimaryKey(supplier.getId()));
			if (!supplier.equals(originSupplier) && supplier.equals(existingSupplier)) {
				mav.addAllObjects(supplierService.loadSupplierClassReferencedSet(supplier));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("supplier/editSupplier.jsp");
			} else {
				supplierService.saveSupplier(supplier);
				mav.setViewName("forward:/listSuppliers");
			}
		}
		return mav;
	}

	@RequestMapping("/supplierController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
